package Q1;

import java.util.Scanner;

class Process {
    String name;
    int arrivalTime;
    int burstTime;

    public Process(String name, int arrivalTime, int burstTime) {
        this.name = name;
        this.arrivalTime = arrivalTime;
        this.burstTime = burstTime;
    }
}

public class Question1 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        Process[] processes = new Process[4];

        // 读取进程信息
        for (int i = 0; i < 4; i++) {
            String line = scanner.nextLine();
            String[] parts = line.split(" ");
            String name = parts[0];
            int arrivalTime = Integer.parseInt(parts[1]);
            int burstTime = Integer.parseInt(parts[2]);
            processes[i] = new Process(name, arrivalTime, burstTime);
        }

        // 执行短作业优先调度
        int currentTime = 0;
        int[] turnaroundTime = new int[4];
        boolean[] finished = new boolean[4];

        for (int count = 0; count < 4; count++) {
            int idx = -1;
            int minBurstTime = Integer.MAX_VALUE;

            // 找到下一个可以执行的进程
            for (int i = 0; i < 4; i++) {
                if (!finished[i] && processes[i].arrivalTime <= currentTime) {
                    if (processes[i].burstTime < minBurstTime) {
                        minBurstTime = processes[i].burstTime;
                        idx = i;
                    } else if (processes[i].burstTime == minBurstTime) {
                        // 按输入顺序选择
                        if (idx == -1 || processes[i].arrivalTime < processes[idx].arrivalTime) {
                            idx = i;
                        }
                    }
                }
            }

            if (idx != -1) {
                // 执行该进程
                currentTime += processes[idx].burstTime;
                turnaroundTime[idx] = currentTime - processes[idx].arrivalTime;
                finished[idx] = true;
            } else {
                // 如果没有可执行的进程，则时间推进到下一个进程到达时刻
                currentTime++;
            }
        }

        // 输出周转时间
        for (int i = 0; i < 4; i++) {
            System.out.print(turnaroundTime[i]);
            if (i < 3) {
                System.out.print(" ");
            }
        }
    }
}